﻿using SheepStorage.Domain.DTO.Output;
using SheepStorage.Domain.Entity;
using SheepStorage.Repository.Interface;
using SheepStorage.Service.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SheepStorage.Service.DataImp
{
    /// <summary>
    /// 羊场栏位服务实现
    /// </summary>
    public class SheepFieldService : ISheepFieldService
    {

        /// <summary>
        /// 栏位仓储
        /// </summary>
        private readonly IBaseRepository<SheepField> repository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="repository">栏位仓储</param>
        public SheepFieldService(IBaseRepository<SheepField> repository)
        {
            this.repository = repository;
        }
        /// <summary>
        /// 添加栏位
        /// </summary>
        /// <param name="sheepField"></param>

        public async Task<int> AddSheepField(SheepField sheepField)
        {
            var Filed = new List<SheepField>();
            if (sheepField.AddType == "单个添加")
            {
                var FiledList = repository.GetAll().FirstOrDefault(c => c.SheepFieldName == sheepField.SheepFieldName);
                if (FiledList != null)
                {
                    return -1; //已存在
                }

                Filed.Add(sheepField);
            }
            else
            {
                var start = int.Parse(sheepField.SheepFieldCode);
                var end = int.Parse(sheepField.SheepFieldNumber);
                for (int i = start; i <= end; i++)
                {
                    var field = new SheepField()
                    {
                        SheepFieldName = sheepField.Prefix + i.ToString(), // 添加前缀
                        Prefix = sheepField.Prefix,
                        SheepFieldCode = sheepField.SheepFieldCode,
                        SheepFieldNumber = sheepField.SheepFieldNumber,
                        BuildingManagementId = sheepField.BuildingManagementId
                    };
                    var fileds = repository.GetAll().FirstOrDefault(c => c.SheepFieldName == sheepField.SheepFieldName);
                    if (fileds != null)
                    {
                        return -1; //已存在
                    }
                    Filed.Add(field);
                }
            }
            return repository.AddRange(Filed);
        }


        /// <summary>
        /// 栏位显示
        /// </summary>
        /// <param name="BuildingManagementId"></param>
        /// <returns></returns>
        public async Task<List<SheepField>> GetSheepField(int BuildingManagementId)
        {
            var list = repository.GetAll().ToList();
            if (BuildingManagementId > 0)
            {
                list=list.Where(c=>c.BuildingManagementId==BuildingManagementId).ToList();
            }
            return list;
        }

     
    }
}
